<template>
    <div>
         <el-form ref="searchForm" :inline="true" :model="searchMap" style="margin-top:20px">
      <!-- 对于需要重置的字段需要加上prop=XX属性 -->
      <el-form-item prop="username">
        <el-input v-model="searchMap.username" placeholder="账号"></el-input>
      </el-form-item>
        <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="fecthStaff">查询</el-button>
        <el-button type="primary"  @click="toAdd">添加</el-button>
        <el-button   @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>
        <el-table border height="400" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="username" label="账号"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="90px"> </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="salary" label="工资"> </el-table-column>
      <el-table-column prop="entrydate" label="入职时间"> </el-table-column>
      <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
     <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增弹出框 -->
    <el-dialog title="会员编辑" :visible.sync="dialogFormVisible" width="500px">
         <el-form :rules="rule" ref="StaffForm" label-width="100px" label-position="right" :model="staff" style="width:400px">
    <el-form-item label="账号" prop="username" >
      <el-input v-model="staff.username" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="姓名" prop="name">
      <el-input v-model="staff.name" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="年龄" prop="age" >
      <el-input v-model="staff.age" autocomplete="off"></el-input>
    </el-form-item>
           <el-form-item label="电话" prop="mobile" >
      <el-input v-model="staff.mobile" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="工资" prop="salary" >
      <el-input v-model="staff.salary" autocomplete="off"></el-input>
    </el-form-item>
        <el-form-item label="入职时间" prop="entrydate" >
      <el-input v-model="staff.entrydate" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="staff.id === null ? addStaff('StaffForm') : edit('StaffForm')">确 定</el-button>
        <!-- <el-button type="primary" @click="addStaff('StaffForm')">确 定</el-button> -->
  </div>
    </el-dialog>
    </div>
</template>


<script>
import StaffApi from '@/api/staff'
export default {
    data() {
        return {
            list:[],
            currentPage:1,
            pageSize:10,
            total:0,
            searchMap:{
                username:"",
                name:"",
            },
            staff:{
                id:null,
                username:"",
                name:"",
                age:0,
                mobile:"",
                salary:0.0,
                entrydate:"",
            },
            dialogFormVisible: false,
            rule:{
                username:[
                    {required: true,message:"请输入账号",trigger:"blur"},
                ],
                name:[
                    {required: true,message:"请输入账号",trigger:"blur"},
                ]
            },
        }
    },
    methods: {
        //新增员工
        addStaff(formName){
            this.$refs[formName].validate(valid=>{
                if(valid){
                    StaffApi.add(this.staff).then(res=>{
                        const result = res.data;
                        if(result.flag){
                            this.fecthStaff();
                            this.dialogFormVisible = false;
                            this.$message({
                                message:result.msg,
                                type:"success",
                            });
                        }else{
                            this.$message({
                            message:result.msg,
                            type:"error",
                            });
                        }
                    })
                }else{
                    return false;
                }
            })
        },
        toAdd(){
            this.dialogFormVisible = true;
            this.$nextTick(()=>{
                this.$refs['StaffForm'].resetFields();
            })
        },
        //重置搜索
        resetForm(formName){
            this.$refs[formName].resetFields();
        },
        //分页或条件查询员工列表
        fecthStaff(){
            StaffApi.search(this.currentPage,this.pageSize,this.searchMap).then(res=>{
                const result = res.data;
                if(result.flag){
                    this.total = result.data.total;
                    this.list = result.data.rows;
                }
            })
        },
        //编辑回显
        handleEdit(id){
            console.log(id);
            this.toAdd();
            StaffApi.getOne(id).then(res=>{
                const result = res.data;
                if(result.flag){
                    this.staff = result.data;
                }
            })
        },
        edit(formName){
            this.$refs[formName].validate(valid=>{
                if(valid){
                    StaffApi.updateOne(this.staff).then(res=>{
                        const result = res.data;
                        if(result.flag){
                            this.dialogFormVisible = false;
                            this.fecthStaff();
                            this.$message({
                                message:result.msg,
                                type:"success",
                            });
                        }else{
                                this.$message({
                                message:result.msg,
                                type:"error",
                            });
                        }
                    })
                }else{
                    return false;
                }
            }) 
        },
        //数据删除
        handleDelete(id){
            console.log(id);
             this.$confirm("此操作将删除商品信息, 是否继续?","提示",{
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(()=>{
              StaffApi.deleteOne(id).then(res=>{
                  const result = res.data;
                  if(result.flag){
                      this.fecthStaff();
                      this.$message({
                            message:result.msg,
                            type:"success",
                      });
                  }else{
                         this.$message({
                            message:result.msg,
                            type:"error",
                      }); 
                  }
              })
          }).catch(()=>{
              //取消
          })
        },
        //每页显示条数更新
        handleSizeChange(val){
            this.pageSize = val;
            this.fecthStaff();
        },
        //页数更新
        handleCurrentChange(val){
            this.currentPage = val;
            this.fecthStaff();
        }
    },
    created() {
        this.fecthStaff();
    },
}
</script>